#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
const int N = 2e5 + 10;
struct Nod {
	int w;
	bool operator < (const Nod& k) {
		int r1 = 0, r2 = 0;
		int m1 = w, m2 = k.w;
		while (m1) {
			int x = m1 % 10;
			r1 += ((x == 0) || (x == 4) || (x == 6) || (x == 9));
			r1 += ((x == 8) * 2);
			m1 /= 10;
		}
		while (m2) {
			int x = m2 % 10;
			r2 += ((x == 0) || (x == 4) || (x == 6) || (x == 9));
			r2 += ((x == 8) * 2);
			m2 /= 10;
		}
		if (r1 == r2) return w < k.w;
		return r1 < r2;
	}
}arr[N];

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> arr[i].w;
	sort(arr + 1, arr + n + 1);
	for (int i = 1; i <= n; i++) cout << arr[i].w << " ";
	return 0;
}